package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.cl.pojo.Department;
import com.cl.service.impl.DepartmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DepartmentController {
    @Autowired
    private DepartmentServiceImpl departmentService;

    @ResponseBody
    @RequestMapping(value = "/dlist",produces = "application/json;charset=utf-8")
    public String query_DList(int page,int limit,String name){
        List<Department> list = departmentService.queryAll(((page-1)*limit),limit);
        int count=departmentService.queryAll(0,100000000).size();
        List<Department> list1 =departmentService.queryByName(name);
/*        String str =JSON.toJSONString(list);*/
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        if (name==null){
            map.put("data",list);
            map.put("count",count);
        }else {
            map.put("data",list1);
            map.put("count",list1.size());
        }
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
    }

    @ResponseBody
    @RequestMapping("/d_add")
    public String addDepartment(Department department){
        department.setEstablish(new Date());
        int n=departmentService.addDepartment(department);
        return JSON.toJSONString(n);
    }

    @ResponseBody
    @RequestMapping("/d_update")
    public String updateDepartment(Department department){
        int n=departmentService.updateDepartment(department);
        return JSON.toJSONString(n);
    }

    @ResponseBody
    @RequestMapping("/d_del")
    public String delDepartment(int id){
        int n=departmentService.delById(id);
        return JSON.toJSONString(n);
    }

}
